<!DOCTYPE html>
<title>Tests the serialization of XML namespaces on attributes, as reported in 'http://crbug.com/248044', bug 248044</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
setup({single_test: true});

window.doc = (new DOMParser()).parseFromString('<outer attr1="value1" xmlns="http://www.example.com"/>', 'text/xml');
// XML-namespaced attributes should get the "xml" prefix.
doc.documentElement.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'id', 'outer');
// Attributes with the same namespace as the element should get a prefix.
doc.documentElement.setAttributeNS('http://www.example.com', 'foo', 'bar');
doc.documentElement.setAttributeNS('http://www.example.com', 'foo2', 'bar2');
// Two attributes with the same namespace and different prefixes shouldn't confuse the serializer.
doc.documentElement.setAttributeNS('http://www.example.com/ns1', 'fizz', 'buzz');
// A prefix declared in an xmlns attribute (coming soon) shouldn't get another xmlns attribute.
doc.documentElement.setAttributeNS('http://www.example.com/ns1', 'prefix1:fizz2', 'buzz2');
// A prefix without a matching xmlns attribute should get one during serialization.
doc.documentElement.setAttributeNS('http://www.example.com/ns2', 'prefix2:name', 'value');
doc.documentElement.setAttributeNS('http://www.example.com/ns2', 'name2', 'value2');
// Namespace declaration for the prefix1:fizz attribute.
doc.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:prefix1', 'http://www.example.com/ns1');
// Element with the same namespace as the parent.
var innerElement = doc.createElementNS("http://www.example.com", 'inner');
doc.documentElement.appendChild(innerElement);
// An attribute with no namespace doesn't need a prefix.
innerElement.setAttributeNS(null, 'id', 'inner');

// Check the DOM construction.
assert_equals(doc.documentElement.getAttributeNS(null, "attr1"), "value1");
assert_equals(doc.documentElement.getAttributeNS("http://www.example.com", "foo"), "bar");
assert_equals(doc.documentElement.getAttributeNS("http://www.example.com", "foo2"), "bar2");
assert_equals(doc.documentElement.getAttributeNS("http://www.example.com/ns1", "fizz"), "buzz");
assert_equals(doc.documentElement.getAttributeNS("http://www.example.com/ns1", "fizz2"), "buzz2");
assert_equals(doc.documentElement.getAttributeNS("http://www.example.com/ns2", "name"), "value");
assert_equals(doc.documentElement.getAttributeNS("http://www.example.com/ns2", "name2"), "value2");
assert_equals(doc.documentElement.getAttributeNS("http://www.w3.org/XML/1998/namespace", "id"), "outer");
assert_equals(doc.querySelector("inner").localName, "inner");
assert_equals(doc.querySelector("inner").namespaceURI, "http://www.example.com");
assert_equals(doc.querySelector("inner").getAttributeNS(null, "id"), "inner");

window.xmlText = (new XMLSerializer()).serializeToString(doc); // exported for debugging
window.parsedDoc = (new DOMParser()).parseFromString(xmlText, 'text/xml');

// Check the serialization result.
assert_equals(parsedDoc.documentElement.getAttributeNS(null, "attr1"), "value1");
assert_equals(parsedDoc.documentElement.getAttributeNS("http://www.example.com", "foo"), "bar");
assert_equals(parsedDoc.documentElement.getAttributeNS("http://www.example.com", "foo2"), "bar2");
assert_equals(parsedDoc.documentElement.getAttributeNS("http://www.example.com/ns1", "fizz"), "buzz");
assert_equals(parsedDoc.documentElement.getAttributeNS("http://www.example.com/ns1", "fizz2"), "buzz2");
assert_equals(parsedDoc.documentElement.getAttributeNS("http://www.example.com/ns2", "name"), "value");
assert_equals(parsedDoc.documentElement.getAttributeNS("http://www.example.com/ns2", "name2"), "value2");
assert_equals(parsedDoc.documentElement.getAttributeNS("http://www.w3.org/XML/1998/namespace", "id"), "outer");
assert_equals(parsedDoc.querySelector("inner").localName, "inner");
assert_equals(parsedDoc.querySelector("inner").namespaceURI, "http://www.example.com");
assert_equals(parsedDoc.querySelector("inner").getAttributeNS(null, "id"), "inner");
done();
</script>
